package com.zxl.yjb.utils;

import android.content.Context;


import com.amap.api.services.district.DistrictItem;
import com.amap.api.services.district.DistrictResult;
import com.zxl.yjb.bean.AreaBean;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import chihane.jdaddressselector.AddressProvider;
import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.Province;
import chihane.jdaddressselector.model.Street;


public class TestAddressProvider implements AddressProvider {

    private Context context;
    private List<AreaBean> provinceList;
    private List<Province> provinceList1 = new ArrayList<>();
    private List<City> cityList = new ArrayList<>();
    private List<County> countyList = new ArrayList<>();
    private List<Street> streetList = new ArrayList<>();
    private DataManager dataManager;
    private ObserverOnNextListener<List<AreaBean>> observerOnNextListener;

    private String provinceCode = "";
    private String cityCode = "";
    private String countyCode = "";

    private PositionSelectListener positionSelectListener;

    public TestAddressProvider(List<AreaBean> provinceList, Context context,PositionSelectListener positionSelectListener) {
        this.provinceList = provinceList;
        this.context = context;
        this.positionSelectListener = positionSelectListener;
    }
    public TestAddressProvider(List<AreaBean> provinceList, Context context) {
        this.provinceList = provinceList;
        this.context = context;
    }

    public interface PositionSelectListener{
        void onProvinceSelected(DistrictItem province);
        void onCitySelected(DistrictItem city);
        void onCountySelected(DistrictItem county);
        void onStreetSelected(DistrictItem street);
    }

    @Override
    public void provideProvinces(AddressReceiver<Province> addressReceiver) {
        provinceList1.clear();
        for (AreaBean data : provinceList) {
            Province province = new Province();
            province.id = Integer.parseInt(data.getCode());
            province.name = data.getName();
            provinceList1.add(province);
        }
        addressReceiver.send(provinceList1);
    }

    @Override
    public void provideCitiesWith(final int provinceId, final AddressReceiver<City> addressReceiver) {
        /*provinceCode = provinceId+"";
        dataManager = new DataManager();
        Map<String,String> map = new HashMap<>();
        map.put("provinceCode",provinceId+"");
        map.put("cityCode","");
        map.put("countyCode","");
        observerOnNextListener = new ObserverOnNextListener<List<AreaBean>>() {
            @Override
            public void onNext(List<AreaBean> nationalBeanList) {
                cityList.clear();
                for (AreaBean data : nationalBeanList) {
                    City city = new City();
                    city.province_id = provinceId;
                    city.id = Integer.parseInt(data.getCode());
                    city.name = data.getName();
                    cityList.add(city);
                }
                addressReceiver.send(cityList);
            }
        };
        dataManager.selectNationalByPid(new ProgressObserver<List<AreaBean>>(observerOnNextListener, context), map);*/

        GetPositionUtil.getPosition(context, provinceId + "", new GetPositionUtil.SearchResult() {
            @Override
            public void onResult(DistrictResult districtResult) {
                if (positionSelectListener!=null)
                {
                    positionSelectListener.onProvinceSelected(districtResult.getDistrict().get(0));
                }
            }

            @Override
            public void onListResult(List<AreaBean> list) {
                cityList.clear();
                for (AreaBean data : list) {
                    City city = new City();
                    city.province_id = provinceId;
                    city.id = Integer.parseInt(data.getCode());
                    city.name = data.getName();

                    cityList.add(city);
                }
                addressReceiver.send(cityList);
            }
        });


    }

    @Override
    public void provideCountiesWith(final int cityId, final AddressReceiver<County> addressReceiver) {
        /*cityCode = cityId+"";
        dataManager = new DataManager();
        Map<String,String> map = new HashMap<>();
        map.put("provinceCode",provinceCode);
        map.put("cityCode",cityId+"");
        map.put("countyCode","");
        observerOnNextListener = new ObserverOnNextListener<List<AreaBean>>() {
            @Override
            public void onNext(List<AreaBean> nationalBeanList) {
                countyList.clear();
                for (AreaBean data : nationalBeanList) {
                    County county = new County();
                    county.city_id = cityId;
                    county.id = Integer.parseInt(data.getCode());
                    county.name = data.getName();
                    countyList.add(county);
                }
                addressReceiver.send(countyList);
            }
        };
        dataManager.selectNationalByPid(new ProgressObserver<List<AreaBean>>(observerOnNextListener, context), map);*/

        GetPositionUtil.getPosition(context, cityId + "", new GetPositionUtil.SearchResult() {
            @Override
            public void onResult(DistrictResult districtResult) {
                if (positionSelectListener!=null)
                {
                    positionSelectListener.onCitySelected(districtResult.getDistrict().get(0));
                }
            }

            @Override
            public void onListResult(List<AreaBean> list) {
                countyList.clear();
                for (AreaBean data : list) {
                    County county = new County();
                    county.city_id = cityId;
                    county.id = Integer.parseInt(data.getCode());
                    county.name = data.getName();
                    countyList.add(county);
                }
                addressReceiver.send(countyList);
            }
        });

    }

    @Override
    public void provideStreetsWith(final int countyId, final AddressReceiver<Street> addressReceiver) {
        /*dataManager = new DataManager();
        dataManager.setParams("pid", countyId + "");
        Map<String,String> map = new HashMap<>();
        map.put("provinceCode",provinceCode);
        map.put("cityCode",cityCode);
        map.put("countyCode",countyId+"");
        observerOnNextListener = new ObserverOnNextListener<List<AreaBean>>() {
            @Override
            public void onNext(List<AreaBean> nationalBeanList) {
                streetList.clear();
                for (AreaBean data : nationalBeanList) {
                    Street street = new Street();
                    street.county_id = countyId;
                    street.id = Integer.parseInt(data.getCode());
                    street.name = data.getName();
                    streetList.add(street);
                }
                addressReceiver.send(streetList);
            }
        };
        dataManager.selectNationalByPid(new ProgressObserver<List<AreaBean>>(observerOnNextListener, context), map);*/

        GetPositionUtil.getPosition(context, countyId + "", new GetPositionUtil.SearchResult() {
            @Override
            public void onResult(DistrictResult districtResult) {
                if (positionSelectListener!=null)
                {
                    positionSelectListener.onCountySelected(districtResult.getDistrict().get(0));
                }
            }

            @Override
            public void onListResult(List<AreaBean> list) {
                streetList.clear();
                for (AreaBean data : list) {
                    Street street = new Street();
                    street.county_id = countyId;
                    street.id = Integer.parseInt(data.getCode());
                    street.name = data.getName();
                    streetList.add(street);
                }
                addressReceiver.send(streetList);
            }
        });
    }
}
